/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.templates.L1ArmorSets;
import l1j.server.server.utils.SQLUtil;
import l1j.server.server.utils.collections.Lists;

public class ArmorSetTable {
	private static Logger _log = Logger.getLogger(ArmorSetTable.class.getName());

	private static ArmorSetTable _instance;

	private final List<L1ArmorSets> _armorSetList = Lists.newList();

	public static ArmorSetTable getInstance() {
		if (_instance == null) {
			_instance = new ArmorSetTable();
		}
		return _instance;
	}

	private ArmorSetTable() {
		load();
	}

	private void load() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {

			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM armor_set");
			rs = pstm.executeQuery();
			fillTable(rs);
		}
		catch (SQLException e) {
			_log.log(Level.SEVERE, "error while creating armor_set table", e);
		}
		finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	private void fillTable(ResultSet rs) throws SQLException {
		while (rs.next()) {
			L1ArmorSets as = new L1ArmorSets();
			as.setId(rs.getInt("id"));
			as.setSets(rs.getString("sets"));
			as.setPolyId(rs.getInt("polyid"));
			as.setEffectEnchantLevel(rs.getInt("effect_enchant_level"));
			as.setEffectStartNumber(rs.getInt("effect_start_number"));
			as.setEffectGroupsOne(rs.getString("effect_groups_1"));
			as.setEffectGroupsTwo(rs.getString("effect_groups_2"));
			as.setEffectGroupsThree(rs.getString("effect_groups_3"));
			as.setEffectGroupsFour(rs.getString("effect_groups_4"));
			as.setEffectAc(rs.getString("effect_1_ac"));
			as.setEffectHp(rs.getString("effect_2_hp"));
			as.setEffectMp(rs.getString("effect_3_mp"));
			as.setEffectHpr(rs.getString("effect_4_hpr"));
			as.setEffectMpr(rs.getString("effect_5_mpr"));
			as.setEffectMr(rs.getString("effect_6_mr"));
			as.setEffectHitHpr(rs.getString("effect_7_hit_hpr"));
			as.setEffectHit(rs.getString("effect_8_hit"));
			as.setEffectDmg(rs.getString("effect_9_dmg"));
			as.setEffectSp(rs.getString("effect_10_sp"));
			as.setEffectStr(rs.getString("effect_11_str"));
			as.setEffectDex(rs.getString("effect_12_dex"));
			as.setEffectCon(rs.getString("effect_13_con"));
			as.setEffectWis(rs.getString("effect_14_wis"));
			as.setEffectCha(rs.getString("effect_15_cha"));
			as.setEffectInt(rs.getString("effect_16_int"));
			as.setEffectDefendWater(rs.getString("effect_17_defend_water"));
			as.setEffectDefendWind(rs.getString("effect_18_defend_wind"));
			as.setEffectDefendFire(rs.getString("effect_19_defend_fire"));
			as.setEffectDefendEarth(rs.getString("effect_20_defend_earth"));

			_armorSetList.add(as);
		}
	}

	public L1ArmorSets[] getAllList() {
		return _armorSetList.toArray(new L1ArmorSets[_armorSetList.size()]);
	}

}
